package com.berte.snake;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.Timer;

public class GamePanel extends JPanel implements KeyListener, ActionListener {
    private int lenth;    //蛇的长度
    private int[] snakeX = new int[600];    //蛇的坐标X
    private int[] snakeY = new int[500];    //坐标Y
    private String dir;        //方向

    private boolean isStart = false;    //游戏开始标志

    private Timer timer = new Timer(100, this);    //定时器刷新

    //定义一个食物
    private int foodx;
    private int foody;
    private Random random = new Random();

    private boolean isFail = false;

    public GamePanel() {
        init();
        this.setFocusable(true);
        this.addKeyListener(this);

        timer.start();
    }

    public void init() {
        lenth = 3;
        dir = "R";
        snakeX[0] = 100;
        snakeY[0] = 100;
        snakeX[1] = 75;
        snakeY[1] = 100;
        snakeX[2] = 50;
        snakeY[2] = 100;

        foodx = 25 + 25 * random.nextInt(34);
        foody = 75 + 25 * random.nextInt(24);

    }

    //画板 画界面  画蛇
    //Graphics  画笔
    @Override
    protected void paintComponent(Graphics arg0) {
        // TODO Auto-generated method stub
        super.paintComponent(arg0); //清屏

        //设置背景的颜色
        this.setBackground(Color.WHITE);
        //绘制头部的广告栏
        Data.header.paintIcon(this, arg0, 25, 11);

        //绘制游戏区域
        arg0.fillRect(25, 75, 850, 600);

        if (dir.equals("R")) {
            Data.snake_right.paintIcon(this, arg0, snakeX[0], snakeY[0]);
        } else if (dir.equals("L")) {
            Data.snake_left.paintIcon(this, arg0, snakeX[0], snakeY[0]);
        } else if (dir.equals("U")) {
            Data.snake_up.paintIcon(this, arg0, snakeX[0], snakeY[0]);
        } else if (dir.equals("D")) {
            Data.snake_down.paintIcon(this, arg0, snakeX[0], snakeY[0]);
        }

        for (int i = 1; i < lenth; i++) {
            Data.snake_body.paintIcon(this, arg0, snakeX[i], snakeY[i]);
        }

        //画食物
        Data.snake_food.paintIcon(this, arg0, foodx, foody);

        if (!isStart) {
            //提示
            arg0.setColor(Color.WHITE);
            arg0.setFont(new Font("微软雅黑", Font.BOLD, 40));
            arg0.drawString("按下空格开始游戏", 300, 300);
        }

        //失败提醒
        if (isFail) {
            arg0.setColor(Color.red);
            arg0.setFont(new Font("微软雅黑", Font.BOLD, 40));
            arg0.drawString("游戏失败，按下空格重新开始", 200, 300);
        }

    }


    //监听键盘的输入
    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        //键盘按下
        int keyCode = e.getKeyCode();

        if (keyCode == KeyEvent.VK_SPACE) {
            if (isFail == true) {
                isFail = false;
                init();
            } else {
                //暂停游戏
                isStart = !isStart;
            }

            repaint();
        }

        //键盘控制走向
        if (keyCode == KeyEvent.VK_LEFT) {
            if (!dir.equals("R")) dir = "L";
        } else if (keyCode == KeyEvent.VK_RIGHT) {
            if (!dir.equals("L")) dir = "R";
        } else if (keyCode == KeyEvent.VK_UP) {
            if (!dir.equals("D")) dir = "U";
        } else if (keyCode == KeyEvent.VK_DOWN) {
            if (!dir.equals("U")) dir = "D";
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
        //键盘弹起
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
        //键盘按下弹起
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        if (isStart && isFail == false) {

            for (int i = lenth - 1; i > 0; i--) {
                snakeX[i] = snakeX[i - 1];
                snakeY[i] = snakeY[i - 1];
            }

            //通过方向让头部移动
            if (dir.equals("R")) {
                snakeX[0] = snakeX[0] + 25;
                if (snakeX[0] > 850) {
                    snakeX[0] = 25;
                }
            } else if (dir.equals("L")) {
                snakeX[0] = snakeX[0] - 25;
                if (snakeX[0] < 25) {
                    snakeX[0] = 850;
                }
            } else if (dir.equals("U")) {
                snakeY[0] = snakeY[0] - 25;
                if (snakeY[0] < 75) {
                    snakeY[0] = 650;
                }
            } else if (dir.equals("D")) {
                snakeY[0] = snakeY[0] + 25;
                if (snakeY[0] > 650) {
                    snakeY[0] = 75;
                }
            }

            if (snakeX[0] == foodx && snakeY[0] == foody) {
                lenth++;

                foodx = 25 + 25 * random.nextInt(34);
                foody = 75 + 25 * random.nextInt(24);
            }

            for (int i = 1; i < lenth; i++) {
                if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
                    isFail = true;
                }

            }

            repaint(); //刷新界面
        }
        timer.start();
    }
}
